Smart meter visualisation using WPD data portal and R libraries & code
#####[NB - run the code within Rstudio and any missing libraries will be loaded/installed via prompt]
##### download WPD open data and convert to data table
url <- 'https://connecteddata.westernpower.co.uk/api/3/action/datastore_search?resource_id=5e531ff5-02ff-48d7-8512-6e603fb569b4&limit=300000'
wpdd <- fromJSON(url, flatten = TRUE)
wpd1 <- wpdd[["result"]][["records"]]
wpd1 <- as.data.table(wpd1)
## calculate percentage smart meters
wpd1$pct_smart <- 100 * (wpd1$SMETS1 + wpd1$SMETS2) / (wpd1$SMETS1 + wpd1$SMETS2 + wpd1$NON_SMART)
boxplot(wpd1[,c(8:10)], outline = F, col="green")
## load wpd geo-polygons
wpe <- st_read("https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/c0bc2a2d-deda-4dc8-8317-0ec8a24af677/download/east-midlands-primary.gpkg")
## Reading layer `East Midlands primary' from data source
## `https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/c0bc2a2d-deda-4dc8-8317-0ec8a24af677/download/east-midlands-primary.gpkg'
## using driver `GPKG'
## Simple feature collection with 395 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 400746 ymin: 220104 xmax: 557570 ymax: 400502
## Projected CRS: OSGB 1936 / British National Grid
wpw <- st_read("https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/547db41e-fb44-423f-9c71-98266b666c38/download/south-wales-primary.gpkg")
## Reading layer `South Wales primary' from data source
## `https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/547db41e-fb44-423f-9c71-98266b666c38/download/south-wales-primary.gpkg'
## using driver `GPKG'
## Simple feature collection with 189 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 171370 ymin: 165580 xmax: 359518 ymax: 283812
## Projected CRS: OSGB 1936 / British National Grid
wps <- st_read("https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/fe4c3f49-7de9-4ebf-bb10-413c50d59f83/download/south-west-primary.gpkg")
## Reading layer `South West primary' from data source
## `https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/fe4c3f49-7de9-4ebf-bb10-413c50d59f83/download/south-west-primary.gpkg'
## using driver `GPKG'
## Simple feature collection with 302 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 87050 ymin: 7020 xmax: 381194 ymax: 188276
## Projected CRS: OSGB 1936 / British National Grid
wpm <- st_read("https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/83c22640-3a54-415f-b253-e51bee3bd146/download/west-midlands-primary.gpkg")
## Reading layer `West Midlands primary' from data source
## `https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/83c22640-3a54-415f-b253-e51bee3bd146/download/west-midlands-primary.gpkg'
## using driver `GPKG'
## Simple feature collection with 216 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 309351.3 ymin: 168633 xmax: 451348 ymax: 369905
## Projected CRS: OSGB 1936 / British National Grid
# merge 4x regions into 1
wpdall <- rbind(wpe,wpw,wps,wpm)
## add NGrid 400 and 275 overhead networks - open data
## note that these data use a different coordinate reference system
n400 <- st_read("ohl400.kml")
## Reading layer `ohl84400' from data source
## `/Users/davidhaw/OneDrive/R analysis/NGRID/WPD_open_data/smart-meters/ohl400.kml'
## using driver `KML'
## Simple feature collection with 448 features and 2 fields
## Geometry type: MULTILINESTRING
## Dimension: XY
## Bounding box: xmin: -4.991359 ymin: 50.37544 xmax: 1.615322 ymax: 55.66299
## Geodetic CRS: WGS 84
n275 <- st_read("ohl275.kml")
## Reading layer `ohl84275' from data source
## `/Users/davidhaw/OneDrive/R analysis/NGRID/WPD_open_data/smart-meters/ohl275.kml'
## using driver `KML'
## Simple feature collection with 310 features and 2 fields
## Geometry type: MULTILINESTRING
## Dimension: XY
## Bounding box: xmin: -4.560872 ymin: 50.91477 xmax: 1.307115 ymax: 55.14076
## Geodetic CRS: WGS 84
wn<- names(wpd1)
wn[7] <- "PRIM_NRID"
names(wpd1) <- wn
wpdall <- left_join(wpdall, wpd1, by=c("PRIM_NRID"))
wpdall$pct_smart <- 100 * (wpdall$SMETS1 + wpdall$SMETS2) / (wpdall$SMETS1 + wpdall$SMETS2 + wpdall$NON_SMART)
wpdall$GSP_NRID <- NULL
wpdall$BSP_NRID <- NULL
wpda_crs <- st_crs(wpdall)
wpd400 <- st_transform(n400, crs=wpda_crs)
wpd275 <- st_transform(n275, crs=wpda_crs)
wpdabb <- st_bbox(wpdall)
bbx <- st_as_sfc(wpdabb)
wpd400 <- st_intersection(wpd400, bbx)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
wpd275 <- st_intersection(wpd275, bbx)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
wpd400 <- st_intersection(wpdall, wpd400)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
wpd275 <- st_intersection(wpdall, wpd275)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
wpd400 <- wpd400[,c(14:16)]
wpd275 <- wpd275[,c(14:16)]
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(wpdall) +
tm_polygons(col = "green",
alpha=0.2,
lwd=0.7,
id="NAME") +
tm_shape(wpd400) +
tm_lines(col = "blue",
alpha=1,
lwd=2.5,
id="NAME") +
tm_shape(wpd275) +
tm_lines(col = "red",
alpha=1,
lwd=2.5,
id="NAME") +
tm_basemap(server = "Esri.WorldStreetMap")